<?php
class Place_BlockController extends Clu_Controller_Action_Mini
{
	
	public function init()
	{
		//require_once (APP_PATH.'/modules/place/Model/Place.php');
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		$this->mPlace = new place_Model_Place();
		$this->mArticle = new article_Model_Service();
		parent::init();
	}
	/*
	public function statusViewPlaceAction()
	{
		$cityName = $this->miniRequest['city_name'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->cityName = $cityName;
		$category_rtype = $this->miniRequest['category_rtype'];
		$this->renderToHolder('block/4/status-view-place.phtml', false, null, $regionId, $boxWeight);
	}
	*/
	public function listCitiesAction()
	{
		$placeId = $this->miniRequest['placeId'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$category_rtype = $this->miniRequest['category_rtype'];
		$this->view->placeId = $placeId;
		if($category_rtype == 90)
		{
			//cities list + paginator
			$p = array('getArticle' => 1, 'rtype' => $category_rtype);
			$paginator = $this->getListPlace($p);
	       	$this->view->paginator = $paginator;
			$this->view->listCities = $paginator->getCurrentItems();
			$this->renderToHolder('block/listCities.phtml', false, null, $regionId, $boxWeight);
		}
	}
	
	public function listRestaurantAction()
	{
//		$placeId = $this->miniRequest['placeId'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$data['rtype'] = $this->miniRequest['category_rtype'];
		$data['type'] = (array)$this->miniRequest['type'];
		//$data['status'] = 'featured';
		//restaurant list + paginator
		$paginator = $this->getListPlace($data);
//		$this->view->placeId = $placeId;
       	$this->view->paginator = $paginator;
		$this->view->list = $paginator->getCurrentItems();
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->list as $p)
		{
			$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
		}
		$this->view->aList = $articleList;
		if($this->miniRequest['page'] != 'view-place')
			$this->renderToHolder('block/4/listRestaurant.phtml', false, null, $regionId, $boxWeight);
		else
			$this->renderToHolder('block/5/listRestaurant.phtml', false, null, $regionId, $boxWeight);
	}
	
	/*
	public function getListRestaurant($data)
	{
		$select = $this->mPlace->search($data);
		$paginator = $this->generatePaginator($select, 1, 10, 5, 'Jumping');
				
      	//cache in $_SESSION
       	$sess = new Zend_Session_Namespace("restaurant_paginator");
       	$sess->place_restaurant = array("restaurant_params" => $data);
        $data = $sess->place_restaurant;
        if(isset($data['page']))
        {
    		$select = $this->mPlace->search($data['restaurant_params']);
       		$paginator = $this->generatePaginator($select, $this->_getParam('page'), 10,5, 'Jumping');
        }
        return $paginator;
	}
	*/
	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($itemsPerPage);
		$paginator->setCurrentPageNumber($pageNumber);
		$paginator->setPageRange($pageRange);
		Zend_Paginator::setDefaultScrollingStyle($style);
		return $paginator;
    }
    
    
	public function relatedUserAction()
    {
    	$id = $this->miniRequest['id'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->id = $id;
		
		$data = array('user_id' => $id);
		$paginatorPlace = $this->getListPlace($data);
       	$this->view->paginator = $paginatorPlace;
		$this->view->listPlaces = $paginatorPlace->getCurrentItems();
		$this->renderToHolder('block/related-to-user.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function getListPlace($data)
    {
    	if(isset($data['item_per_page']) == null)
			$data['item_per_page'] = 10 ;
		if(isset($data['page']) == null)
			$data['page'] = 1 ;
        $select = $this->mPlace->search($data);
        $paginator = $this->generatePaginator($select, $data['page'], $data['item_per_page'], 5, 'Jumping');
                     
          //cache in $_SESSION
        $sess = new Zend_Session_Namespace("paginator");
        $sess->place_list = array("search_params" => $data);
    	$data = $sess->place_list;
	    if(isset($data['page']))
	    {
	        $select = $this->mPlace->search($data['search_params']);
	        $paginator = $this->generatePaginator($select, $this->_getParam('page'), $data['item_per_page'],5, 'Jumping');
	    }
    	return $paginator;
    }
    
    public function browseByCategoryAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];

		$this->view->rtype = array(20, 18, 4, 38, 21,17,19,6, 5);
		$this->view->place_perpage = 5;
		$this->view->place_page_number = 5;
		$this->view->clu_search = 1;
//		$this->renderToHolder('block/'.$regionId.'/browse-by-categories.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function simpleSearchAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
    	$form = new place_Form_PlaceSearch();
        $form->cluBuild(array('action' => 'search', 'view' => $this->view, 'cluSearchMode' => 'simpleSearch'));
        if(isset($this->miniRequest['data']))
        	$data = $this->miniRequest['data'];
        else
        	$data = null;

        $form->populateCategory($data);
        $this->view->form = $form;
		$this->renderToHolder('block/4/simple-search.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function featuredListAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$data = array('item_per_page' => 6,'status' => 'featured');
		$paginator = $this->getListPlace($data);
       	$this->view->paginator = $paginator;
       	$results = $paginator->getCurrentItems();
		$hcm = $results[0];
		//$battrang = $results[5];
		$results[0] = $results[1]; // 0 = Dalat
		$results[1] = $results[5]; //1 = Bat trang
		$results[5] = $hcm;
		unset($results[2]); //leave out Phuquoc
//		die_a($results);
       	//gallery info for each place, and kept in a separate array 
       	$placeGalleries = array();
		foreach($results as $p)
		{
	       	$contentType = 'place';
			$id = $p['id'];
			$fileType = 'image'; 
			$userId = $p['poster_id'];
	       	if (!$contentType || !$id || !$fileType)
	       		return;
				//die("contentType or id or fileType is not specified. Die!!");
			//find the directory
			$dir = APP_PATH."/public/ufiles/$userId/images/original/$contentType/";
			if(file_exists($dir))
			{
			    $files = scandir($dir);
			    $displayFiles = array();
			    foreach ($files as $f)
			    {
			    	if (strpos ($f, $id) === 0)
			    		$displayFiles[] = $f;
			    }
			    $placeGalleries[$id]['displayFiles'] = $displayFiles;
			    $placeGalleries[$id]['base'] = "/public/ufiles/$userId/images/%s/$contentType/";
			}
		}
		
		$this->view->placeList = $results;
		
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->placeList as $p)
		{
			$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
		}
		$this->view->aList = $articleList;
	
		$this->view->placeGalleries = $placeGalleries;
    	if ( $this->miniRequest['page'] == 'home')
    		$this->renderToHolder('block/featured-place-list-home.phtml', false, null, $regionId, $boxWeight);
	}
	
 	public function placeInCityAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$cityid =  $this->miniRequest['city_id'];
		$rtype	=  $this->miniRequest['category_rtype'];
		
        $data = array('action' => 'search', 'city_id' => $cityid, 'item_per_page' => 5,'rtype' =>$rtype);
		$paginatorPlace = $this->getListPlace($data);
       	$this->view->paginator = $paginatorPlace;
		$this->view->listPlaces = $paginatorPlace->getCurrentItems();
        $this->view->id =  $this->miniRequest['placeId'];
		$this->renderToHolder('block/5/place-list-in-city.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function  topCityMarqueeAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->renderToHolder('block/4/top-city.phtml', false, null, $regionId, $boxWeight);
    }
    
	public function topInCityAction()
    {
    	$regionId 	= $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$cityid =  $this->miniRequest['city_id'];
		$type1  =  $this->miniRequest['type1'];
		$type2 	=  $this->miniRequest['type2'];
		
		//data in the first box
		$data1 = array('action' => 'search', 'city_id' => $cityid, 
				'item_per_page' => 5,'type' =>$type1,
				'status' => "featured");
		$paginator1 = $this->getListPlace($data1);
		$this->view->list1 = $paginator1->getCurrentItems();
		
		//data in the second box
		$data2 = array('action' => 'search', 'city_id' => $cityid,
				'item_per_page' => 5,'type' =>$type2,
				'status' => "featured");
		$paginator2 = $this->getListPlace($data2);
		$this->view->list2 = $paginator2->getCurrentItems();
		$this->renderToHolder('block/4/top-in-city.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function travelInfoAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$block = $this->miniRequest['block'];
		$category =  $this->miniRequest['category'];
		$this->view->category = $category;
		$this->view->clu_search = 1;	
		if($block == 4)
		{
			if($category == 6)
			{
        		$this->view->rtype = array(17,22,20,18,16,19,21);
    			$this->renderToHolder('block/4/actraction_travel.phtml');
			}
			elseif($category == 5)		
			{
        		$this->view->rtype = array(24, 81, 23, 28, 26,17,82,27, 25);
    			$this->renderToHolder('block/4/travel_info.phtml');
			}
		}
    	elseif($block == 5)
		{
			/*
			if($category == 6)
			{
        		$this->view->rtype = array(17,22,20,18,16,19,21);
    			$this->renderToHolder('block/5/actraction-travel.phtml');
			}
			else */
			if($category == 5)		
			{
        		$this->view->rtype = array(24, 81, 23, 28, 26,17,82,27, 25);
    			$this->renderToHolder('block/5/travel-info.phtml');
			}
			
		}   
    }
    
    public function listNatureAction()
	{
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$data['rtype'] = $this->miniRequest['rtype'];
		//restaurant list + paginator
		foreach($data['rtype'] as $v)
		{
			if($v == 18)
			{
				$a['rtype'] = $v;
				$paginator = $this->getListPlace($a);
		       	$this->view->paginator = $paginator;
				$this->view->list = $paginator->getCurrentItems();
				$articleList = array();
				$mArticle = new article_Model_Service();
				foreach ($this->view->list as $p)
				{
					if($p['type'] == 40)
						$articleList[] = $mArticle->getArticleData($p['article_id']);
				}
				$this->view->aList = $articleList;
			}
			elseif($v == 17)
			{
				$a['rtype'] = $v;
				$paginator = $this->getListPlace($a);
		       	$this->view->paginator = $paginator;
				$this->view->cultureList = $paginator->getCurrentItems();
				$articleList = array();
				$mArticle = new article_Model_Service();
				foreach ($this->view->cultureList as $p)
				{
					$articleList[] = $mArticle->getArticleData($p['article_id']);
				}
				$this->view->aCultureList = $articleList;
			}
			elseif($v == 20)
			{
				$a['rtype'] = $v;
				$paginator = $this->getListPlace($a);
		       	$this->view->paginator = $paginator;
				$this->view->resList = $paginator->getCurrentItems();
				$articleList = array();
				$mArticle = new article_Model_Service();
				foreach ($this->view->resList as $p)
				{
					$articleList[] = $mArticle->getArticleData($p['article_id']);
				}
				$this->view->aResList = $articleList;
			}
			elseif($v == 21)
			{
				$a['rtype'] = $v;
				$paginator = $this->getListPlace($a);
		       	$this->view->paginator = $paginator;
				$this->view->shopList = $paginator->getCurrentItems();
				$articleList = array();
				$mArticle = new article_Model_Service();
				foreach ($this->view->shopList as $p)
				{
					$articleList[] = $mArticle->getArticleData($p['article_id']);
				}
				$this->view->aShopList = $articleList;
			}
		}
		if($this->miniRequest['page'] == 'home')
			$this->renderToHolder('block/4/listNature.phtml', false, null, $regionId, $boxWeight);
		else if($this->miniRequest['page'] == 'place')
			$this->renderToHolder('block/4/listNaturePlace.phtml', false, null, $regionId, $boxWeight);
	}
	
	public function listHotelAction()
	{
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$data['rtype'] = $this->miniRequest['rtype'];
		//restaurant list + paginator
		$paginator = $this->getListPlace($data);
       	$this->view->paginator = $paginator;
       	$results = $paginator->getCurrentItems();
		$this->view->list = $paginator->getCurrentItems();
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->list as $p)
		{
			$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
		}
		$this->view->aList = $articleList;
		$this->renderToHolder('block/4/listHotel.phtml', false, null, $regionId, $boxWeight);
	}
	
    public function tabMenuAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->page = $this->miniRequest['page'];
		
	    if(!empty($this->miniRequest['id']))
	    {
	    	$id = $this->miniRequest['id'];
		    $commentRequest = array(
	        						'module' => 'comment',
	        						'controller' => 'block',
	        						'action' => 'moduleComment',
	        						'regionId' => 4,
	        						'boxWeight' => 8,
	        						'id' => $id,
	        						'comments_per_page' => 10,
	        						'page_number' => 1,
	        						'parent_content_type' => 'place' 
	        						);
	        $this->addMiniAction($commentRequest);

			//block event					
			if(!empty($this->miniRequest['uid']))
			{
				$eventRequest = array(
						'module' => 'event',
						'controller' => 'block',
						'action' => 'relatedPlace',
						'regionId' => 4,
						'boxWeight' => 8,
						'placeId' => $id,
						'uid' => $this->miniRequest['uid']
				);
				$this->addMiniAction($eventRequest);
			}
			
			//Q&A
			$eventRequest = array(
					'module' => 'common',
					'controller' => 'block',
					'action' => 'questionAnswer',
					'regionId' => 4,
					'boxWeight' => 8,
					'placeId' => $id,
			);
			$this->addMiniAction($eventRequest);
	    }
		
		if($this->view->page != 'place')
			$this->view->type = $this->miniRequest['type'];
		
		$this->renderToHolder('block/4/tabMenu.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function subMenuAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->page = $this->miniRequest['page'];
		$this->renderToHolder('block/4/subMenu.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function viewPlaceAction() 
    {
        $placeId = $this->miniRequest["id"];
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$dataDisplay = $this->miniRequest['data'];
		$sess = new Zend_Session_Namespace(CLU_NS);
        if(!$dataDisplay)
        {
            $this->view->errorMessage = t(724);
            $this->renderToHolder( 'common/error.phtml');	
        }
        if(!empty($dataDisplay))
        {
	        //get data for display
	        if ($dataDisplay['article_data']['article_language'] != $sess->viewLang)
	            $this->view->errorMessage = sprintf(t(519),  $sess->viewLang );
	        $place['article'] = $dataDisplay['article_data'];
	        $this->view->place = $dataDisplay;
	        $this->view->categoryName = $dataDisplay['category_name']['name'];
	        $a = array();
	        if(isset($dataDisplay['type_name']))
	        {
		        foreach($dataDisplay['type_name'] as $k => $v)
		        {
			        $a[] = $v['name'];
		        }
	        }
	        $this->view->typeName = $a;
	        $this->view->address = unserialize($dataDisplay['metadata']);
	        $this->view->lang = $sess->viewLang;
	        $this->view->oriLang = $dataDisplay['article_data']['article_language'];
	        $this->renderToHolder('block/view-place.phtml', false, null, $regionId, $boxWeight);
        }
    }
    
    public function advertiseAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->page = $this->miniRequest['page'];
		$this->renderToHolder('block/4/advertise.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function neighborhoodAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		
		$this->renderToHolder('block/4/advertise.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function topPlaceListAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		//$this->view->page = $this->miniRequest['page'];
		$listPlace  = array('status' => 'published','item_per_page' => 4);
		$paginatorPlace = $this->getListPlace($listPlace);
       	$this->view->paginator = $paginatorPlace;
		$this->view->pList = $paginatorPlace->getCurrentItems();
		
		//get articles
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->pList as $p)
		{
			$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
		}
		$this->view->aList = $articleList;
		$this->renderToHolder('block/5/top-place-list.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function listCategoryOfCityAction()
	{
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$data['city_id'] = $this->miniRequest['city_id'];
		$data['district_id'] = $this->miniRequest['district_id'];
		//restaurant list + paginator
		$paginator = $this->getListPlace($data);
       	$this->view->paginator = $paginator;
//       	die_a($paginator->getCurrentItems());
		$this->view->list = $paginator->getCurrentItems();
		for( $i = 0; $i < count($this->view->list); $i++)
		{
			if(in_array($this->view->list[$i]['rtype'], array(7,8,9,10,11)))
				$countHotel[] = $this->view->list[$i]['rtype'];
			if(in_array($this->view->list[$i]['rtype'], array(21)))
				$countShop[] = $this->view->list[$i]['rtype'];
			if($this->view->list[$i]['rtype'] == 24)
				$countATM_Bank[] = $this->view->list[$i]['rtype'];
//			if($this->view->list[$i]['rtype'] == 21)
//				$countShop[] = $this->view->list[$i]['rtype'];
//			if($this->view->list[$i]['rtype'] == 21)
//				$countShop[] = $this->view->list[$i]['rtype'];
//			if($this->view->list[$i]['rtype'] == 21)
//				$countShop[] = $this->view->list[$i]['rtype'];
		}
		if(!empty($countHotel))
			$this->view->countHotel = count($countHotel);
		if(!empty($countShop))
			$this->view->countShop = count($countShop);
		if(!empty($countATM_Bank))
			$this->view->countATM_Bank = count($countATM_Bank);
		$this->view->cityName = $this->miniRequest['city_name'];
		$this->view->districtName = $this->miniRequest['district_name'];
		$this->renderToHolder('block/4/list-category-of-city.phtml', false, null, $regionId, $boxWeight);
	}
}
?>